// Package car_park_api
// @Author Spume
// @Date 2024-12-19

package car_park_api

import "fmt"

// CarParkError 停车场API错误接口
type CarParkError interface {
	error
	Code() string
	Message() string
}

// BaseError 基础错误结构
type BaseError struct {
	code    string
	message string
	cause   error
}

func (e *BaseError) Error() string {
	if e.cause != nil {
		return fmt.Sprintf("%s: %s (caused by: %v)", e.code, e.message, e.cause)
	}
	return fmt.Sprintf("%s: %s", e.code, e.message)
}

func (e *BaseError) Code() string {
	return e.code
}

func (e *BaseError) Message() string {
	return e.message
}

func (e *BaseError) Unwrap() error {
	return e.cause
}

// 具体错误类型

// InvalidParamsError 参数无效错误
type InvalidParamsError struct {
	*BaseError
}

func NewInvalidParamsError(message string) *InvalidParamsError {
	return &InvalidParamsError{
		BaseError: &BaseError{
			code:    "INVALID_PARAMS",
			message: message,
		},
	}
}

// APIError API调用错误
type APIError struct {
	*BaseError
	StatusCode int
}

func NewAPIError(statusCode int, message string, cause error) *APIError {
	return &APIError{
		BaseError: &BaseError{
			code:    "API_ERROR",
			message: message,
			cause:   cause,
		},
		StatusCode: statusCode,
	}
}

// ParseError 数据解析错误
type ParseError struct {
	*BaseError
}

func NewParseError(message string, cause error) *ParseError {
	return &ParseError{
		BaseError: &BaseError{
			code:    "PARSE_ERROR",
			message: message,
			cause:   cause,
		},
	}
}

// SignError 签名错误
type SignError struct {
	*BaseError
}

func NewSignError(message string) *SignError {
	return &SignError{
		BaseError: &BaseError{
			code:    "SIGN_ERROR",
			message: message,
		},
	}
}

// NetworkError 网络错误
type NetworkError struct {
	*BaseError
}

func NewNetworkError(message string, cause error) *NetworkError {
	return &NetworkError{
		BaseError: &BaseError{
			code:    "NETWORK_ERROR",
			message: message,
			cause:   cause,
		},
	}
}